package com.cskaoyan.service;

import com.cskaoyan.bean.bo.WxCommentPostBo;
import com.cskaoyan.bean.entity.Comment;
import com.cskaoyan.bean.entity.CommentExample;
import com.cskaoyan.bean.entity.User;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Auther Wenjian_Yang
 * @Date 2021/10/19
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public BasePageData query(Integer userId, Integer valueId, BaseParam param) {

       //配置分页信息
        PageHelper.startPage(param.getPage(),param.getLimit());
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();

        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }

        commentExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        return BasePageData.ok(comments);
    }

    //删除评论信息
    @Override
    public void delete(Comment comment) {
        commentMapper.deleteByPrimaryKey(comment.getId());
    }

    /**
     * 查询数量
     * @param valueId
     * @return
     */
    @Override
    public WxCommentCountVo count(Integer valueId, Integer type) {

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);

        // 查询该商品所有评论的数量
//        List<Comment> comments = commentMapper.selectByExample(commentExample);
        Integer allCount = Math.toIntExact(commentMapper.countByExample(commentExample));

        // 查询该商品所有有图评论的数量
        criteria.andHasPictureEqualTo(true);
        Integer hasPicCount = Math.toIntExact(commentMapper.countByExample(commentExample));
//        boolean isHasPicTrue = false;
//        Integer hasPicCount = 0;
//        if (type == 0){     // 没有图片
//            isHasPicTrue = true;
//            criteria.andHasPictureEqualTo(false);
//            hasPicCount = Math.toIntExact(commentMapper.countByExample(commentExample));
//        }

        return new WxCommentCountVo(allCount,hasPicCount);
    }

    /**
     * 查询全部评论
     * @param valueId
     * @param type
     * @param limit
     * @param page
     * @param showType
     * @return
     */
    @Override
//    public BasePageData list(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {
    public List<WxCommentListVo> list(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {

        //配置分页信息 → 在执行预编译过程，会帮你拼接分页的sql语句

        List<WxCommentListVo> commentListVos = new ArrayList<>();
        WxCommentListVo commentListVo;

        // 获取该商品的全部评论
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        commentExample.setOrderByClause("`id` desc ");
        criteria.andValueIdEqualTo(valueId);
        byte value = type.byteValue();
        criteria.andTypeEqualTo(value);
//        List<Comment> comments = commentMapper.selectByExample(commentExample);
        List<Comment> comments = null;


        PageHelper.startPage(page,limit);
        // 查询该商品所有有图评论的数量
        if (showType == 1){     // 有图片
            criteria.andHasPictureEqualTo(true);
            comments = commentMapper.selectByExample(commentExample);
        }else {   // 所有评论
            comments = commentMapper.selectByExample(commentExample);
        }

        // 存储用户Id
        ArrayList<Integer> userIds = new ArrayList<>();
        // 查询所有用户信息
        for (Comment comment : comments) {
            Integer tempUserId = comment.getUserId();   // 每一个评论的用户id
            // 获取该评论的用户信息
            User user = userMapper.selectByPrimaryKey(tempUserId);
            WxCommentListVo.UserInfo userInfo = new WxCommentListVo.UserInfo(user.getNickname(),user.getAvatar());
//            String addTime = comment.getAddTime().toString();
            commentListVo = new WxCommentListVo(userInfo,comment.getAddTime(),comment.getPicUrls(),comment.getAdminContent(),comment.getContent());
            commentListVos.add(commentListVo);
        }

//        return BasePageData.ok(commentListVos);
        return commentListVos;
    }

    // 发表专题评论
    @Override
    public WxCommentPostVo post(WxCommentPostBo wxCommentPostBo, String username) {
//    public Comment post(WxCommentPostBo wxCommentPostBo, String username) {

        // 获取用户id
        Integer userId = userMapper.selectByUsername(username);

        // 添加专题评论
        Date date = new Date();
//        Comment comment = new Comment(valueId,type,wxCommentPostBo.getContent(),wxCommentPostBo.getHasPicture(),strings,star,date,userId);
        Comment comment = new Comment(wxCommentPostBo.getValueId(),wxCommentPostBo.getType(),wxCommentPostBo.getContent(),wxCommentPostBo.getHasPicture(),wxCommentPostBo.getPicUrls(),wxCommentPostBo.getStar(),date,userId);
        int affecter = commentMapper.insertSelective(comment);
        System.out.println(affecter);

        // 获取最后插入的自增id值
        Integer id = commentMapper.selectId();
        // 查询新插入的评论
        Comment comment1 = commentMapper.selectByPrimaryKey(id);

        WxCommentPostVo wxCommentPostVo = new WxCommentPostVo(comment1.getId(), comment1.getValueId(), comment1.getType(), comment1.getContent(), comment1.getUserId(), comment1.getHasPicture(), comment1.getPicUrls(), comment1.getStar(), comment1.getAddTime(), comment1.getUpdateTime());

        return wxCommentPostVo;
//        return comment1;

    }


}
